package com.eedi.framework.info.controller.org;

import cn.hutool.core.bean.BeanUtil;
import com.eedi.framework.info.controller.org.vo.*;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.info.controller.org.vo.*;
import com.eedi.framework.info.service.OrgInfoService;
import com.eedi.framework.org.service.SysOrgConfigService;
import com.eedi.framework.web.core.util.WebFrameworkUtils;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;

@Tag(name = "经营界面 - 机构管理")
@RestController
@RequestMapping("/info")
@Validated
@Slf4j
public class OrgInfoController {


    @Resource
    OrgInfoService orgInfoService;

    @Resource
    SysOrgConfigService sysOrgConfigService;

    @PostMapping("/create")
    @Operation(summary = "创建机构(org:info:crate)")
    @PreAuthorize("@org.hasPermission('org:info:crate')")
    public CommonResult<OrgInfoResp> create(@RequestBody OrgInfoCreateReq req) {
        return success(
                orgInfoService.create(req)
        );
    }

    @PutMapping("/update")
    @Operation(summary = "修改机构(org:info:update)")
    @PreAuthorize("@org.hasPermission('org:info:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody OrgInfoUpdateReq updateReqVO) {
        orgInfoService.update(updateReqVO);
        return success(true);
    }

    @PutMapping("/disable")
    @Operation(summary = "停用机构(org:info:update)")
    @Parameter(name = "orgInfoId", description = "机构编号", required = true)
    @PreAuthorize("@org.hasPermission('org:info:update')")
    public CommonResult<Void> disable(@RequestParam("orgInfoId") String orgInfoId) {
        orgInfoService.disableByOrgInfoId(orgInfoId);
        return success(null);
    }

    @PutMapping("/enable")
    @Operation(summary = "启用机构(org:info:update)")
    @Parameter(name = "orgInfoId", description = "机构编号", required = true)
    @PreAuthorize("@org.hasPermission('org:info:update')")
    public CommonResult<Void> enable(@RequestParam("orgInfoId") String orgInfoId) {
        orgInfoService.enableByOrgInfoId(orgInfoId);
        return success(null);
    }

    @GetMapping("/page")
    @Operation(summary = "机构列表(org:info:list)")
    @PreAuthorize("@org.hasPermission('org:info:list')")
    public CommonResult<PageResult<OrgInfoResp>> list(OrgInfoPageReq req) {
        return success(orgInfoService.page(req));
    }

    @GetMapping("/details")
    @Operation(summary = "获取机构通用配置全部信息")
    @PermitAll
    public CommonResult<OrgConfigAllResp> details() {
        OrgInfoResp orgInfoResp = this.orgInfoService.getByOrgInfoId(WebFrameworkUtils.getOrgInfoId());
        OrgConfigAllResp  orgConfigAllResp = BeanUtil.toBean(sysOrgConfigService.getAllDetails(),OrgConfigAllResp.class);
        orgConfigAllResp.setOrgInfoResp(orgInfoResp);
        return success(orgConfigAllResp);
    }


    @GetMapping("/detail")
    @Operation(summary = "获取当前机构详细信息")
    public CommonResult<OrgInfoResp> detail() {
        return success(orgInfoService.getByOrgInfoId(WebFrameworkUtils.getOrgInfoId()));
    }


}
